package com.jie.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jie.commonutils.ResultEntity;
import com.jie.eduservice.entity.EduCourse;
import com.jie.eduservice.entity.EduTeacher;
import com.jie.eduservice.service.EduCourseService;
import com.jie.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 前台系统中用于返回名师数据的Controller类
 * @author XieZhiJie
 * @create 2020-10-17 11:09
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/eduservice/teacherfront")
public class TeacherFrontController {

    private final EduTeacherService teacherService;

    private final EduCourseService courseService;

    @Autowired
    public TeacherFrontController(
            EduTeacherService teacherService,
            EduCourseService courseService
    ) {
        this.teacherService = teacherService;
        this.courseService = courseService;
    }

    /**
     * 1.分页查询讲师的方法
     * @param current  当前页
     * @param limit    每页多少条数据
     * @return         返回查询到的数据
     */
    @PostMapping("/getTeacherFrontList/{current}/{limit}")
    public ResultEntity getTeacherFrontList(
            @PathVariable("current") Long current,
            @PathVariable("limit") Long limit
    ) {
        Map<String, Object> map = teacherService.getTeacherFrontList(current, limit);
        return ResultEntity.ok().data(map);
    }

    /**
     * 2.查询讲师详情
     * @param teacherId  要查询详情的讲师信息
     * @return           返回这个讲师的详情信息
     */
    @GetMapping("/getTeacherFrontInfo/{teacherId}")
    public ResultEntity getTeacherFrontInfo(
            @PathVariable("teacherId") String teacherId
    ) {
        // 1.根据讲师id查询讲师信息
        EduTeacher eduTeacher = teacherService.getById(teacherId);

        // 2.根据讲师id查询所讲的课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", teacherId);
        List<EduCourse> courseList = courseService.list(wrapper);

        return ResultEntity.ok().data("teacher", eduTeacher).data("courseList", courseList);
    }

}
